<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\string\StringUtils;

/**
 * manager_role action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class ManagerRoleAction extends CommonAction {

    protected $serviceBean = 'admin.role.service';
    protected $actionTitle = '管理员角色';

    //数据列表页面
    public function index( HttpRequest $request ) {
        $name = $request->getParameter('name', 'trim');

        $conditions = array();
        if ( $name ) $conditions['name'] = "%{$name}%";

        $this->setConditions($conditions);
        parent::index($request);
        $this->setView('system/role_index');
        $this->assign('breadcrumb', array('系统管理', '管理员角色列表'));
    }

    //数据添加页面
    public function add(HttpRequest $request) {
        $this->loadPermissions();
        $this->setView('system/role_add');
        $this->assign('breadcrumb', array('系统管理', '管理员角色添加'));
    }

    //数据编辑页面
    public function edit(HttpRequest $request) {
        parent::edit($request);
        $item = $this->getTemplateVar('item');
        $permissions = StringUtils::jsonDecode($item['permissions']);
        $this->assign('permissions', $permissions);
        $this->loadPermissions();
        $this->setView('system/role_edit');
        $this->assign('breadcrumb', array('系统管理', '管理员角色编辑'));
    }

    //加载权限数据
    public function loadPermissions() {
        $service = Beans::get('system.operation.service');
        $operations = $service->getOperationsByGroup();
        //__print($operations);die();
        $this->assign('operations', $operations);
    }

    //插入数据操作
    public function insert(HttpRequest $request) {
        $data = $request->getParameter('data');
        parent::__insert($data);
    }

    //更新数据操作
    public function update(HttpRequest $request) {

        $data = $request->getParameter('data');
        $permissions = $request->getParameter('permissions');
        $data['permissions'] = StringUtils::jsonEncode($permissions);
        $id = $request->getParameter('id', 'trim');
        parent::__update($data, $id);

    }
}
